/**
 * Copyright (c) 2011-2016, Eason Pan(pylxyhome@vip.qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jcbase.controller.sys;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.jcbase.core.auth.anno.RequiresPermissions;
import com.jcbase.core.controller.JCBaseController;
import com.jcbase.core.model.Condition;
import com.jcbase.core.model.Operators;
import com.jcbase.core.pojo.ObjDetail;
import com.jcbase.core.util.CommonUtils;
import com.jcbase.core.util.IWebUtils;
import com.jcbase.core.view.InvokeResult;
import com.jcbase.model.SysConfig;
import com.jcbase.model.SysObj;
import com.jcbase.model.SysObjColletc;
import com.jcbase.model.SysUser;
import com.jfinal.plugin.activerecord.Page;

/**
 * 系统对象管理
 * 
 * @author eason
 */
public class ObjColletcController extends JCBaseController {

    @RequiresPermissions(value = { "/sys/objColletc" })
    public void index() {
	render("obj_collect.jsp");
    }

    @RequiresPermissions(value = { "/sys/objColletc" })
    public void getCollectObjView() {
	render("obj_collect.jsp");
    }

    @RequiresPermissions(value = { "/sys/objColletc" })
    public void addCollect() {
	String ids = this.getPara("ids");
	SysUser user = IWebUtils.getCurrentSysUser(getRequest());
	// 查询是否
	boolean result = SysObjColletc.insertShareObj(user, ids);
	this.renderJson(InvokeResult.success(result));

    }

    @RequiresPermissions(value = { "/sys/objColletc" })
    public void delCollect() {
	String ids = this.getPara("ids");
	SysUser user = IWebUtils.getCurrentSysUser(getRequest());
	// 查询是否
	boolean result = SysObjColletc.me.delCollect(user, ids);
	this.renderJson(InvokeResult.success(result));

    }

    // 获取我的收藏
    @RequiresPermissions(value = { "/sys/objColletc" })

    public void getCollectObjList() {
	String name = this.getPara("name");
	SysUser user = IWebUtils.getCurrentSysUser(getRequest());
	Set<Condition> conditions = new HashSet<Condition>();
	if (CommonUtils.isNotEmpty(name)) {
	    conditions.add(new Condition("name", Operators.LIKE, name));
	}
	Page<SysObj> pageInfo;

	pageInfo = SysObj.me.getPage(this.getPage(), this.getRows(), "select * ",
		"from sys_obj where id in (select obj_id from sys_obj_colletc where user_id = " + user.getId() + ")",
		this.getOrderby());

	user = SysUser.me.findById(user.getId());
	List<SysObj> objList = pageInfo.getList();
	SysConfig host = SysConfig.me.findById(1);
	for (SysObj sysObj : objList) {
	    StringBuffer sb = new StringBuffer();
	    sb.append("<a href = '#' onclick = \"forword('");
	    sb.append(host.getValue());
	    sb.append(sysObj.getLink());

	    String common = user.getParams();
	    if (common != null && !"".equals(common)) {
		sysObj.setParams(common);
		String params[] = common.split(",");
		for (String param : params) {
		    sb.append("&");
		    sb.append(param);
		}
	    }

	    sb.append("')\">");
	    sb.append(sysObj.getLink());
	    sb.append("</a>");
	    sysObj.setLink(sb.toString());
	}
	this.renderJson(SysObj.me.toJqGridView(pageInfo));
    }

    public Page<ObjDetail> changeBean(Page<ObjDetail> pageInfo, SysUser user) {

	List<ObjDetail> objList = pageInfo.getList();
	SysConfig host = SysConfig.me.findById(1);
	for (ObjDetail sysObj : objList) {
	    StringBuffer sb = new StringBuffer();
	    sb.append("<a href = '#' onclick = \"forword('");
	    sb.append(host.getValue());
	    sb.append(sysObj.getLink());

	    String common = user.getParams();
	    if (common != null && !"".equals(common)) {
		sysObj.setParams(common);
		String params[] = common.split(",");
		for (String param : params) {
		    sb.append("&");
		    sb.append(param);
		}
	    }

	    sb.append("')\">");
	    sb.append(sysObj.getLink());
	    sb.append("</a>");
	    sysObj.setLink(sb.toString());
	}
	return pageInfo;
    }
}
